package com.lsx.logisticscore.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.entity.Account;
import com.lsx.logisticscore.mapper.AccountMapper;
import com.lsx.logisticscore.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private AccountService accountService;

    /**
     * admin/manager创建账号
     * @param accountParam
     * @return
     */
    @PostMapping("/register")
    public CommonResp<Account> doRegister(@RequestBody String accountParam){
        Account account = JSONObject.parseObject(accountParam, Account.class);
        CommonResp<Account> resp = new CommonResp<>();
        Account newAccount = accountService.saveAccount(account);
        if (newAccount == null){
            resp.setStatus("-1");
            resp.setMessage("创建失败");
            return resp;
        }
        newAccount.setPassword(null);
        resp.setContent(newAccount);
        return resp;
    }

    @RequestMapping("/list")
    public CommonResp<Page<Account>> getAccountList(Page<Account> page, String username, String type, String empId){
        return new CommonResp<>(accountMapper.selectAll(page, username, type, empId));
    }
    /**
     * 输入注册码激活账号
     * @param registerCode
     * @return 注册码对应的账号信息
     *
     */
    @RequestMapping("/active")
    public CommonResp<Account> doActiveAccount(String registerCode, String empId){
        log.info("参数为：{}，{}", registerCode, empId);
        CommonResp<Account> resp = new CommonResp<>();
        Account account = accountService.activeAccount(registerCode, empId);
        if (null == account){
            resp.setMessage("注册码错误");
        }
        resp.setContent(account);
        return resp;
    }

    @PostMapping("/init")
    public CommonResp<Account> doInitAccount(@RequestBody JSONObject params){
        log.info("传入的参数为：{}", params);
        Account account = new Account();
        account.setEmpId(params.getString("empId"));
        account.setPassword(params.getString("pwd1"));
        CommonResp<Account> resp = new CommonResp<>();
        Account account1 = accountService.initAccount(account);
        resp.setContent(account1);
        return resp;
    }
    /**
     * 对于账号来说,更新就是改密码
     * @param params
     * @return
     */
    @RequestMapping("/update")
    public CommonResp<Integer> addAccount(@RequestBody JSONObject params){
        //有点麻烦,需要走加密的流程,晚点再搞
        int result = accountService.changePassword(params);
        CommonResp<Integer> resp = new CommonResp<>(result);
        return resp;
    }

    @RequestMapping("/getByEmpId")
    public CommonResp<Account> getAccountByEmpId(String empId){
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id", empId);
        return new CommonResp<>(accountMapper.selectOne(wrapper));
    }

    @RequestMapping("/remove")
    public CommonResp<Integer> removeAccountById(@RequestBody JSONObject accountId){
        int result = accountService.deleteAccount(accountId.getInteger("accountId"));
        CommonResp<Integer> resp = new CommonResp<>();
//        if (result == -1){
//            resp.setStatus("1-");
//            resp.setMessage("员工存在");
//        }
        resp.setContent(result);
        return resp;
    }

}
